<template>
  <div class="register-page">
    <!-- 头部 -->
    <div class="header">
      <div class="back" @click="$router.back()">
        <i class="iconfont icon-back"></i>
      </div>
      <div class="title">用户注册</div>
    </div>

    <!-- 注册表单 -->
    <div class="register-form">
      <div class="form-item">
        <label>手机号：</label>
        <input type="tel" v-model="form.userId" placeholder="请输入手机号" @blur="checkUserId">
      </div>
      <div class="form-item">
        <label>密码：</label>
        <input type="password" v-model="form.password" placeholder="请输入密码">
      </div>
      <div class="form-item">
        <label>确认密码：</label>
        <input type="password" v-model="confirmPassword" placeholder="请再次输入密码">
      </div>
      <div class="form-item">
        <label>用户名：</label>
        <input type="text" v-model="form.userName" placeholder="请输入用户名">
      </div>
      <div class="form-item">
        <label>性别：</label>
        <div class="radio-group">
          <div class="radio" :class="{ active: form.userSex === 1 }" @click="form.userSex = 1">
            <span>先生</span>
          </div>
          <div class="radio" :class="{ active: form.userSex === 0 }" @click="form.userSex = 0">
            <span>女士</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 注册按钮 -->
    <div class="btn-group">
      <div class="register-btn" @click="register">注册</div>
      <div class="login-btn" @click="$router.push('/login')">已有账号，去登录</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const form = ref({
  userId: '',
  password: '',
  userName: '',
  userSex: 1
})
const confirmPassword = ref('')

// 检查用户ID是否已存在
const checkUserId = async () => {
  if (!form.value.userId) return

  try {
    const res = await axios.get('http://localhost:8080/elm/user/getUserById', {
      params: { userId: form.value.userId }
    })
    if (res.data > 0) {
      alert('该手机号已被注册')
      form.value.userId = ''
    }
  } catch (error) {
    console.error('检查用户ID失败:', error)
  }
}

// 注册
const register = async () => {
  // 表单验证
  if (!form.value.userId) {
    alert('请输入手机号')
    return
  }
  if (!form.value.password) {
    alert('请输入密码')
    return
  }
  if (!confirmPassword.value) {
    alert('请确认密码')
    return
  }
  if (form.value.password !== confirmPassword.value) {
    alert('两次输入的密码不一致')
    return
  }
  if (!form.value.userName) {
    alert('请输入用户名')
    return
  }

  try {
    const res = await axios.post('http://localhost:8080/elm/user/saveUser', null, {
      params: form.value
    })
    if (res.data > 0) {
      alert('注册成功')
      router.replace('/login')
    } else {
      alert('注册失败')
    }
  } catch (error) {
    console.error('注册失败:', error)
  }
}
</script>

<style scoped>
.register-page {
  padding-top: 44px;
  background: #f5f5f5;
  min-height: 100vh;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  z-index: 100;
}

.back {
  padding: 10px;
  margin-left: -10px;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 16px;
}

.register-form {
  background: #fff;
  margin-top: 20px;
}

.form-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.form-item label {
  width: 80px;
  color: #333;
}

.form-item input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 14px;
}

.radio-group {
  flex: 1;
  display: flex;
}

.radio {
  padding: 5px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  margin-right: 10px;
  font-size: 14px;
  color: #666;
}

.radio.active {
  background: #0097ff;
  color: #fff;
  border-color: #0097ff;
}

.btn-group {
  padding: 20px 15px;
}

.register-btn {
  height: 44px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 22px;
  font-size: 16px;
  margin-bottom: 15px;
}

.login-btn {
  height: 44px;
  background: #fff;
  color: #0097ff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 22px;
  font-size: 16px;
  border: 1px solid #0097ff;
}
</style>